inline
edt_feat::edt_feat(const std::string in_actionNames,  
		   const uword in_col, 
		   const uword in_row,
		   const int in_dim
)
// //actionNames, co, ro, run, 
:actionNames(in_actionNames), col(in_col), row(in_row), dim(in_dim)
{
  THRESH_2 = col*row*1/100;
  actions.load( actionNames );  //actions.print("All actions");
  ismultiAction = false; // per default is false
  
}


inline
void
edt_feat::features_allpeople ( field <string> people, string single_path, string single_path_mask ) 
{
 for (uword pe = 0; pe< people.n_rows; ++pe)
  {
    for (uword act = 0; act< actions.n_elem; ++act)
    {
      //feature_one_video
      feature_one_video.clear(); 
      std::stringstream ssVideo_path;
      ssVideo_path << single_path <<  actions(act) << "/" << people(pe) << "_" << actions(act)<< ".avi";
      
      std::stringstream ssVideo_path_mask;
      ssVideo_path_mask << single_path_mask <<  actions(act) << "/" << people(pe) << "_" << actions(act)<< ".avi";
      
      //cout << ssVideo_path.str()<< endl;
      
      cout << "Calculating features vector per video :" << ssVideo_path.str() << endl;
      //feature_video( ssVideo_path.str(), "/media/johanna/HD1T/codes/datasets_codes/weizmann/mask/run/daria_run.avi" ) ; 
      feature_video( ssVideo_path.str(), ssVideo_path_mask.str() ) ; //feat_all_video_action_i is calculated inside this method
      cout << "# of Vectors: " << feature_one_video.size() << endl;
      
      cout << "Converting to Arma:" << endl;
      cout << "# of Vectors: " << feature_one_video.size() << endl;
      
      mat mat_features(dim,feature_one_video.size());
      
      for (uword i = 0; i < feature_one_video.size(); ++i)
      {
	mat_features.col(i) = feature_one_video.at(i)/norm(feature_one_video.at(i),2);
	
      }
      
      std::stringstream ssName_feat_video;
      ssName_feat_video << "./edt_features_training/edt_feat_vec_" << people(pe) << "_" << actions(act);
      cout << "Saving at " << ssName_feat_video.str() << endl;
      mat_features.save( ssName_feat_video.str(), raw_ascii );
      //mat_features.print();
      //getchar();
    }
  }
}


//*******************************************************************************************
//***************************Feature per video: Training and Testing ************************

inline 
void
edt_feat::feature_video( string one_video, string one_video_mask )//quitar one video
{
  
  std::vector < vec > feat_frame;
  
  
  //cv::VideoCapture capVideo(one_video);//creo que no lo necesito
  cv::VideoCapture capVideo_mask(one_video_mask);
    
  //double frmcount = capVideo.get(CV_CAP_PROP_FRAME_COUNT);
  double frmcount = capVideo_mask.get(CV_CAP_PROP_FRAME_COUNT);
  //cout << "# of frames is: " << frmcount << endl;
  
  if( !capVideo_mask.isOpened() ) //!capVideo.isOpened() 
  {
    cout << "Video couldn't be opened" << endl;
    return;
  }
  

  cv::Mat  mask, mask_rgb, eucl_T; //gray mask as I don't know how to open a VIDEO IN GRAYSCALES
 
  //Saving each two frames
  int len = ceil(frmcount/2)-1; //0 and 1 not used
  //featuresframe_video_i.set_size( len );
  //cout << "Len " << len << endl;
  //lab_feature_vectors.set_size( frmcount );
  
  
  for(uword fr=0; fr<frmcount; fr++){
    
     bool bSuccess2= capVideo_mask.read(mask_rgb); // read a new frame from video
     
     cvtColor( mask_rgb, mask, CV_BGR2GRAY );
     //cv::imshow("Dios", mask);
     cv::Mat tmp_mask;
     mask.copyTo(tmp_mask);

    //bool bSuccess = capVideo.read(frame); // read a new frame from video
    
    //if (!bSuccess) //if not success, break loop
	//{
	  //cout << "Cannot read the frame from video file" << endl;
	  //break;
	//}
    //cv::cvtColor(frame, gray, CV_BGR2GRAY);
    //cv::cvtColor(frame,gray, cv::COLOR_BGR2GRAY);//For Opencv 3.0 (installed at home)

      if (fr>0)
      {
	///Finding the Bounding Box
	vector<vector<cv::Point> > contours;
	vector<cv::Vec4i> hierarchy;
	cv::findContours( tmp_mask, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
	//cout << "Number of contours: " << contours.size() << endl;
	cv::Rect bb;
	int largest_area=0;
	int largest_contour_index=0;
	
	// iterate through each contour. 
	for( int i = 0; i< contours.size(); i++ ) 
	{
	  double a=contourArea( contours[i],false);  //  Find the area of contour
	  
	  if(a>largest_area){
	    largest_area=a;
	    largest_contour_index=i;                //Store the index of largest contour
	    bb=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
	}
      }
      //end finding BB
      
      ///Creating bb_mask
      cv::Mat bb_mask(mask, bb);
      //cv::imshow("bb1", bb_mask);
      ///Resizing Image
      cv::resize(bb_mask,bb_mask, cv::Size(col,row) );
      cv::distanceTransform(bb_mask, eucl_T, CV_DIST_L2, 5 );
      cv::normalize(eucl_T, eucl_T, 0.0, 1.0, cv::NORM_MINMAX);
      cv::Mat eucl_Tt(eucl_T.t());
      
      fmat eucl_Tra(eucl_Tt.ptr<float>(), row, col); //Arma


	fvec fv = vectorise(eucl_Tra);
	vec v = conv_to< vec >::from(fv);
	feature_one_video.push_back(v);
	
	if (ismultiAction)     
	{
	  uword lab = arma_multi_labels(fr);
	  lab_feature_vectors.push_back( lab ); 
	  
	}


// 	int fontFace = cv::FONT_HERSHEY_PLAIN;
// 	double fontScale = 0.8;
// 	int thickness = 1;  
// 	cv::Point textOrg(10, 100);
// 	cv::putText(frame, text, textOrg, fontFace, fontScale, cv::Scalar::all(255), thickness,8);
//	cout << "otro tam: " <<feat_video_i.size() << endl;

	//Showing images :)
// 	cv::rectangle(mask_rgb,bb,cv::Scalar(255,255,0),2);
//  	cv::imshow("mask_rgb", mask_rgb ); 
//  	cv::imshow("mask", mask); 
//  	cv::imshow("bb", bb_mask);
//  	cv::imshow("EuclideanTransform", eucl_T);
//  	cv::waitKey();

      }

  }

}

//******************TESTING MULTI ACTIONS***********************************
//***************************************************************************

inline 
void 
edt_feat::feature_multi_action(field <string> peo_test, string multi_path, string multi_path_mask)
{
  ismultiAction = true; // per default is false
  
  for (uword vi = 0; vi <peo_test.n_rows; ++vi ){ //videos.n_rows

      feature_one_video.clear(); 
      lab_feature_vectors.clear();
     
      
      std::stringstream ssTest;
      ssTest << multi_path << peo_test(vi) << "_multiactions.avi";
      cout << ssTest.str()<< endl;
      
      std::stringstream ssTest_mask;
      ssTest_mask << multi_path_mask << peo_test(vi) << "_multiactions.avi";
      cout << ssTest_mask.str()<< endl;
      
      std::stringstream tmp_lbs;
      tmp_lbs << multi_path << peo_test(vi) << "_labels.dat";
      cout << tmp_lbs.str() << endl;
      arma_multi_labels.load(tmp_lbs.str(), raw_ascii); //labels are in a frame basis.
      
      
      
      cout << "Calculating features vector per video ..." << peo_test(vi) << endl;;
      feature_video( ssTest.str(), ssTest_mask.str() ); //feature_one_video is calculated inside this method
      
      
      
      //cout << "Total feature vectors: " << feature_one_video.size()<< endl;
      //cout << "Total labels: " << lab_feature_vectors.size() << endl;
      
      cout << "Converting to Arma:" << endl;
      cout << "# of Frames: "  << lab_feature_vectors.size() << endl;
      cout << "# of Vectors: " << feature_one_video.size() << endl;
      
      uvec lab_feature_vectors_arma( lab_feature_vectors.size() );
      mat  feature_vectors_arma( dim, feature_one_video.size() );
      
      
      
      for (uword i = 0; i < feature_one_video.size(); ++i)
      {
	//cout << i << endl;
	feature_vectors_arma.col(i) = feature_one_video.at(i)/norm(feature_one_video.at(i),2);
	
	//mat_features.col(i) = feat_all_videos_action_i.at(i)/norm(feat_all_videos_action_i.at(i),2);
	
      }
      
      for (uword i = 0; i < lab_feature_vectors.size(); ++i)
      {
	//cout << i << endl;
	lab_feature_vectors_arma(i) = lab_feature_vectors.at(i);
	
	
      }

      std::stringstream tmp_ss4;
      tmp_ss4     << "./edt_multi_features_testing/edt_feat_"     <<  peo_test(vi) << ".dat";  
      
      std::stringstream tmp_vec_lab;
      tmp_vec_lab << "./edt_multi_features_testing/edt_lab_feat_"      <<  peo_test(vi) << ".dat";  
      
      
      //cout << tmp_ss4.str() << endl;
      //cout << "Press a key " << endl;
      //getchar();
      cout << "Saving " << endl;
      feature_vectors_arma.save( tmp_ss4.str(), raw_ascii );
      lab_feature_vectors_arma.save( tmp_vec_lab.str(), raw_ascii );
      //cout << "Press a key" << endl;
      //getchar();
      
      
    }

}


